package cc.linker.steplink.crm.merchanth5.controller.acs;

import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.service.acs.AgentOperateGoodsService;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;


@RestController
@RequestMapping("/h5/agentgoods")
public class AgentOperateGoodsController {

    @Autowired
    private AgentOperateGoodsService agentOperateGoodsService;

    @Autowired
    private RequestFilter requestFilter;

    /**
     * 加盟代理上架或者下架
     * @param request
     * @throws GlobalException
     */
    @RequestMapping("/shelfonoroff")
    public void addGoods(@RequestBody Map<String,Object> param, HttpServletRequest request) throws GlobalException {
        String customerId = this.findCustomerId(request);
//        String s = agentOperateGoodsService.checkCustomerType(customerId);
//        String s="promoter";
//        String customerId="A20190527ot0o1j";
            param.put("customerId",customerId);
            //判断这是添加操作还是移除操作
            if ("1".equals(param.get("isShelf"))){
                //检查商品是否已经添加过要代理的商品
                int i = agentOperateGoodsService.checkIsAdd(param);
                if (i==0){
                    Date date = new Date();
                    param.put("updateTime",date);
                    agentOperateGoodsService.addGoods(param);
                }else {
                    //有添加过该商品历史记录，状态重新改为1即可
                    agentOperateGoodsService.changeRemoveStatus(param);
                }
            }else {
                //删除操作为逻辑删除，更改移除状态
                agentOperateGoodsService.changeRemoveStatus(param);
            }
    }

    /**
     * 上下架商品
     * @param param
     * @param request
     *//*
    @RequestMapping("/shelfonoroff")
    public void shelfGoods(@RequestBody Map<String,Object> param,HttpServletRequest request){
        String customerId = findCustomerId(request);
        if (null!=param){
            param.put("customerId",customerId);
            agentOperateGoodsService.shelfGoods(param);
        }
    }*/


    private String findCustomerId(HttpServletRequest request){
        WxUser wxUser =  requestFilter.getWxUser(request);
        return wxUser.getCustomerId();
    }
}
